package com.zhipu.balance.utils.print.soybean

import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.mobile.event.RecordSoybean
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.print.BrightekCommandM
import java.nio.charset.Charset

/**
 * 大豆计算
 */
object SoybeanPrintUtils {

    /**
     * 大豆计算
     */
    fun SoybeanPrinting(mRecordSoybean : RecordSoybean){
        val stringBuilder = StringBuilder()
        val title = "大豆计算"
        val retract = ((1.0 - title.length / 16.0) * 16).toInt()
        for (i in 0 until retract) {
            stringBuilder.append(" ")
        }
        stringBuilder.append(title).append('\r')
        //编号
        //编号
        stringBuilder.append("编号：").append(mRecordSoybean.uniqueId).append('\r')
        //时间
        //时间
        stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordSoybean.inspectionDate)).append('\r')
        /**
         * 杂质记录员
         */
        if(!TextUtils.isEmpty(mRecordSoybean.impurityRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("杂质").append('\r')
            stringBuilder.append("\r")
            // 大样试验-试样质量
            stringBuilder.append("大样质量(G)：").append(mRecordSoybean.largeMiscellaneousSampleQuality).append('\r');
            //大样试验-杂质
            stringBuilder.append("大样杂质(G)：").append(mRecordSoybean.detailImpurity).append('\r');
            //大样杂质率
            stringBuilder.append("大样杂质率(%)：").append(mRecordSoybean.detailResults).append('\r');
            //小样试验-试样质量
            stringBuilder.append("小样质量(G)：").append(mRecordSoybean.smallMiscellaneousSampleQuality).append('\r');
            //小样试验-杂质
            stringBuilder.append("小样杂质(G)：").append(mRecordSoybean.smallSampleImpurity).append('\r');
            //小样杂质率
            stringBuilder.append("小样杂质率(%)：").append(mRecordSoybean.sampleResults).append('\r');
            //杂质总量
            stringBuilder.append("杂质总量(%)：").append(mRecordSoybean.impurityResults).append('\r');
            //杂质记录员
            stringBuilder.append("杂质记录员：").append(mRecordSoybean.impurityRecorder).append('\r');
        }
        //热损伤粒记录员
        if(!TextUtils.isEmpty(mRecordSoybean.thermalDamageGrainRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("热损伤粒").append('\r');
            stringBuilder.append("\r");
            //热损伤粒质量
            stringBuilder.append("热损伤粒质量(G)：").append(mRecordSoybean.thermalDamageGrainQuality).append('\r');
            //热损伤粒率
            stringBuilder.append("热损伤粒率(%)：").append(mRecordSoybean.thermalDamageGrain).append('\r');
            //热损伤粒记录员
            stringBuilder.append("热损伤粒记录员：").append(mRecordSoybean.thermalDamageGrainRecorder).append('\r');
        }


        //损伤粒记录员
        if(!TextUtils.isEmpty(mRecordSoybean.damagedGrainRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("损伤粒").append('\r');
            stringBuilder.append("\r");

            //损伤粒(含热损伤粒)质量
            stringBuilder.append("损伤粒(含热损伤粒)质量(G)：").append(mRecordSoybean.damagedGrainQuality).append('\r');
            //损伤粒率
            stringBuilder.append("损伤粒率(%)：").append(mRecordSoybean.damagedGrainResult).append('\r');
            //损伤粒率记录员
            stringBuilder.append("损伤粒率记录员：").append(mRecordSoybean.damagedGrainRecorder).append('\r');
        }
        //完整粒率记录员
        if(!TextUtils.isEmpty(mRecordSoybean.wholeGrainRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("完整粒率").append('\r');
            stringBuilder.append("\r");
            //未熟粒、破损粒质量
            stringBuilder.append("未熟粒、破损粒质量(G)：").append(mRecordSoybean.immatureBrokenGrainQuality).append('\r');
            //完整粒率
            stringBuilder.append("完整粒率(G)：").append(mRecordSoybean.wholeGrainRate).append('\r');
            //完整粒率记录员
            stringBuilder.append("完整粒率记录员：").append(mRecordSoybean.wholeGrainRecorder).append('\r');
        }

        var i = 0
        while (i < (1.0 - 2 / 16.0) * 16) {
            stringBuilder.append(" ")
            i++
        }
        InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
        InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
    }


    /**
     * 大豆计算
     */
    fun SoybeanPrinting(mRecordSoybeanList: List<RecordSoybean>,mSubclass: String){
        for (mRecordSoybean in mRecordSoybeanList){
            if(!mRecordSoybean.select){
                continue
            }
            val stringBuilder = StringBuilder()
            val title = "大豆计算"
            val retract = ((1.0 - title.length / 16.0) * 16).toInt()
            for (i in 0 until retract) {
                stringBuilder.append(" ")
            }
            stringBuilder.append(title).append('\r')
            //编号
            stringBuilder.append("编号：").append(mRecordSoybean.uniqueId).append('\r')
            //时间
            stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordSoybean.inspectionDate)).append('\r')
            when (mSubclass) {
                "杂质" -> {
                    /**
                     * 杂质记录员
                     */
                    if(!TextUtils.isEmpty(mRecordSoybean.impurityRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("杂质").append('\r')
                        stringBuilder.append("\r")
                        // 大样试验-试样质量
                        stringBuilder.append("大样质量(G)：").append(mRecordSoybean.largeMiscellaneousSampleQuality).append('\r');
                        //大样试验-杂质
                        stringBuilder.append("大样杂质(G)：").append(mRecordSoybean.detailImpurity).append('\r');
                        //大样杂质率
                        stringBuilder.append("大样杂质率(%)：").append(mRecordSoybean.detailResults).append('\r');
                        //小样试验-试样质量
                        stringBuilder.append("小样质量(G)：").append(mRecordSoybean.smallMiscellaneousSampleQuality).append('\r');
                        //小样试验-杂质
                        stringBuilder.append("小样杂质(G)：").append(mRecordSoybean.smallSampleImpurity).append('\r');
                        //小样杂质率
                        stringBuilder.append("小样杂质率(%)：").append(mRecordSoybean.sampleResults).append('\r');
                        //杂质总量
                        stringBuilder.append("杂质总量(%)：").append(mRecordSoybean.impurityResults).append('\r');
                        //杂质记录员
                        stringBuilder.append("杂质记录员：").append(mRecordSoybean.impurityRecorder).append('\r');
                    }
                }
                "损伤粒率" -> {
                    //热损伤粒记录员
                    if(!TextUtils.isEmpty(mRecordSoybean.thermalDamageGrainRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("热损伤粒").append('\r');
                        stringBuilder.append("\r");
                        //热损伤粒质量
                        stringBuilder.append("热损伤粒质量(G)：").append(mRecordSoybean.thermalDamageGrainQuality).append('\r');
                        //热损伤粒率
                        stringBuilder.append("热损伤粒率(%)：").append(mRecordSoybean.thermalDamageGrain).append('\r');
                        //热损伤粒记录员
                        stringBuilder.append("热损伤粒记录员：").append(mRecordSoybean.thermalDamageGrainRecorder).append('\r');
                    }
                }
                "热损伤粒率" -> {
                    //损伤粒记录员
                    if(!TextUtils.isEmpty(mRecordSoybean.damagedGrainRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("损伤粒").append('\r');
                        stringBuilder.append("\r");

                        //损伤粒(含热损伤粒)质量
                        stringBuilder.append("损伤粒(含热损伤粒)质量(G)：").append(mRecordSoybean.damagedGrainQuality).append('\r');
                        //损伤粒率
                        stringBuilder.append("损伤粒率(%)：").append(mRecordSoybean.damagedGrainResult).append('\r');
                        //损伤粒率记录员
                        stringBuilder.append("损伤粒率记录员：").append(mRecordSoybean.damagedGrainRecorder).append('\r');
                    }
                }
                "完整粒率" -> {
                    //完整粒率记录员
                    if(!TextUtils.isEmpty(mRecordSoybean.wholeGrainRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("完整粒率").append('\r');
                        stringBuilder.append("\r");
                        //未熟粒、破损粒质量
                        stringBuilder.append("未熟粒、破损粒质量(G)：").append(mRecordSoybean.immatureBrokenGrainQuality).append('\r');
                        //完整粒率
                        stringBuilder.append("完整粒率(G)：").append(mRecordSoybean.wholeGrainRate).append('\r');
                        //完整粒率记录员
                        stringBuilder.append("完整粒率记录员：").append(mRecordSoybean.wholeGrainRecorder).append('\r');
                    }
                }
            }
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
            InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
        }

    }
}